package com.xiang.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 *
 * @author: xiangtianlei
 * @Date: 2022-9-2
 */
@Configuration
public class RabbitMqTopicConfig {
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
    public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
    public static final String ROUTINGKEY_SMS = "inform.#.sms.#";

    /**
     * 声明交换机(exchangeTopics)
     *
     * @return org.springframework.amqp.rabbit.annotation.Exchange
     */
    @Bean
    public Exchange exchangeTopic() {
        //  durable(true)，持久化交换机，重启服务之后还存在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    /**
     * 声明队列（sms）
     *
     * @return org.springframework.amqp.core.Queue
     */
    @Bean
    public Queue DeclareQueueSMS() {
        return new Queue(QUEUE_INFORM_SMS);
    }

    /**
     * 声明队列（email）
     *
     * @return org.springframework.amqp.core.Queue
     */
    @Bean
    public Queue DeclareQueueEmail() {
        return new Queue(QUEUE_INFORM_EMAIL);
    }

    /**
     * 绑定队列到交换机(email->exchangeTopics)
     *
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding BindingQueueEmail(@Qualifier("DeclareQueueEmail") Queue queue, @Qualifier("exchangeTopic") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
    }

    /**
     * 绑定队列到交换机（sms->exchangeTopics）
     *
     * @param queue, exchange
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding BindingQueueSms(@Qualifier("DeclareQueueSMS") Queue queue, @Qualifier("exchangeTopic") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
    }
}
